#include "glviewport.h"

GLViewport::GLViewport(QWidget *parent) :
    QGLWidget(parent), modelo(0)
{
    left_button=false;
    right_button=false;
    lx=ly=0;
    zoom = 6;
    angx = angy = 0;
}

void GLViewport::initializeGL(){
    qglClearColor(QColor::fromRgbF(0,0,0));
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glDisable(GL_CULL_FACE);
    glEnable(GL_BLEND);



    //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

}

void GLViewport::resizeGL(int w, int h){
    //int side = min(w, h);
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    gluPerspective(45,w/float(h),0.1,100);


    glMatrixMode(GL_MODELVIEW);


}

void GLViewport::paintGL(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
    glTranslatef( 0, 0, -zoom );
    glRotatef(angx,1,0,0);
    glRotatef(angy,0,1,0);
    if(!modelo)
        return;

    for(int c=0; c<modelo->caras.count(); c++){
        Cara cara = modelo->caras[c];
        QVector<Vertice> verts;
        for(int a=0; a<cara.aristas.count(); a++){
            Arista arista = modelo->aristas_map[cara.aristas[a]];
            Vertice v1 = modelo->vert_transform_map[arista.v1];
            int count = verts.count(v1);
            if(count==0){
                verts.append(v1);
            }

            Vertice v2 = modelo->vert_transform_map[arista.v2];
            count = verts.count(v2);
            if(count==0){
                verts.append(v2);
            }
        }

        Color color=modelo->colors_transform[c];
        glColor3f(color.r,color.g,color.b);

        glBegin(GL_POLYGON);
        for(int v=0;v<verts.count();v++){
            glVertex3f(verts[v].x,verts[v].z,verts[v].y);
        }
        glEnd();
    }


}

void GLViewport::mousePressEvent(QMouseEvent *event){
    if(event->button()==Qt::LeftButton){
        left_button=true;
        lx = event->x();
        ly = event->y();
    }

    if(event->button()==Qt::RightButton){
        right_button=true;
        lx = event->x();
        ly = event->y();
    }
}

void GLViewport::mouseMoveEvent(QMouseEvent *event){
    if(left_button){
        int mx = event->x();
        int my = event->y();
        int dx = mx-lx;
        int dy = my-ly;
        angx += dy/5.0;
        angy += dx/5.0;

        lx = mx;
        ly = my;
    }else if(right_button){
        int mx = event->x();
        int my = event->y();
        int dx = mx-lx;
        int dy = my-ly;
        float dist=dy;
        zoom+=dist/20.0;

        if(zoom<3)
            zoom=3;
        if(zoom>16)
            zoom=16;

        lx = mx;
        ly = my;
    }

    updateGL();


}

void GLViewport::mouseReleaseEvent(QMouseEvent *event){
    if(event->button()==Qt::LeftButton){
        left_button=false;
    }

    if(event->button()==Qt::RightButton){
        right_button=false;
    }
}

void GLViewport::SetModelo(Modelo *modelo_){
    modelo = modelo_;
    updateGL();
}


